#include <iostream>
#include "CL_News_pool.h"

CL_News_pool* CL_News_pool::news_pool_singleton = NULL;
time_t CL_News_pool::last_update = 0;
time_t CL_News_pool::interval = 300;
time_t CL_News_pool::max_age = 18000;  /*5 hours */
newsmap CL_News_pool::mynewsmap;
std::map<std::string,CL_RSS_Quelle> CL_News_pool::sources;
key_pair CL_News_pool::last_key_pair;
int CL_News_pool::adaption_rate=0;

CL_News_pool& CL_News_pool::get_news_pool()
{
    if(!news_pool_singleton)
        news_pool_singleton=new CL_News_pool();
    return *news_pool_singleton;
};

bool CL_News_pool::add_source_url(const std::string& s)
{
    if(!CL_News_pool::sources[s])
    {
        sources[s].set_url(s);
        newsmap tm;
        tm+=sources[s];
        delete_old_news(tm);
        mynewsmap.insert(tm.begin(),tm.end());
    }
    return sources[s];
};

CL_News CL_News_pool::old_news_first()
{
    update();
    if(!mynewsmap.size()) return CL_News("Believe me: no news are good news");
    if(last_key_pair.t==0)
    {
        last_key_pair=mynewsmap.begin()->first;
        return mynewsmap.begin()->second;
    };
    newsmap::iterator it=mynewsmap.find(last_key_pair);
    if(it==mynewsmap.end())
    {
        adaption_rate=-1;
        it=mynewsmap.begin();
#warning TODO (markus#1#): Nur für Debugging!\

        std::cout<<"##################################################"<<std::endl;
        std::cout<<"Schneller!"<<std::endl;
        std::cout<<"##################################################"<<std::endl;
    } /*news had been deleted unseen
                                    cause the calling program is to slow: inform the calling programm */
    else
    {
        it++;
        if(it==mynewsmap.end())
        {
            /*news are read to fast; we have to do a roundabout */
#warning TODO (markus#1#): Nur für Debugging!\
std::cout<<"##################################################"<<std::endl;
            std::cout<<"Langsamer!"<<std::endl;
            std::cout<<"##################################################"<<std::endl;
            adaption_rate=1;
            it=mynewsmap.begin();
        }
    }
    last_key_pair=it->first;
    return it->second;
};
CL_News CL_News_pool::random_news()
{

};
int CL_News_pool::get_news_count()
{

};

int CL_News_pool::get_adaption_rate()
{
    int t=adaption_rate;
    adaption_rate=0;
    return t;
};
void CL_News_pool::update()
{
    time_t at=time(NULL);
    if((at-last_update)<interval)return;
    last_update=at;
    force_update();
};
void CL_News_pool::force_update()
{
    for(std::map<std::string,CL_RSS_Quelle>::iterator it=sources.begin(); it!=sources.end(); it++)
    {
        mynewsmap+=it->second;    //put all new news from the url CL_RSS_Quelle is pointing to into internal memory
    };
    /*delete whatever is older than max_age; remember that the first elements in the map are the oldest*/
    delete_old_news(mynewsmap);
};

void CL_News_pool::delete_old_news(newsmap& nm)
{
    key_pair kp;
    kp.t=time(NULL)-max_age;
    kp.guid=0;
    newsmap::iterator it=nm.upper_bound(kp);
    if(it!=nm.begin())
        nm.erase(nm.begin(),it);
};
